﻿using System;
using System.Collections.Generic;
using System.Linq;
using DLM.Domain.Model;
using DLM.Domain.Repositories;
using DLM.Infrastructure.UnitOfWork;
using DLM.Infrastructure.Domain;
using Webdiyer.WebControls.Mvc;
using DLM.Domain.DTO;

namespace DLM.Data.EF.CodeFirst.Repositories
{
    public class SalutationRepository : Repository<Salutation, int>, ISalutationRepository
    {
        public SalutationRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }


        public IEnumerable<Domain.DTO.SalutationDTO> FindSalutationByAccount(string accountName, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name");
            }
            var result = from u in this.ActiveContext.User
                         join s in this.ActiveContext.Salutation
                         on u.UserID equals s.UserID
                         join nu in this.ActiveContext.User
                         on s.GreetID equals nu.UserID
                         where u.AccountName == accountName
                         select new SalutationDTO
                         {
                             SentDate = s.SentDate,
                             SID = s.SID,
                             GreetAccount = nu.AccountName,
                             GreetID = s.GreetID,
                             GreetName = nu.Name,
                             GreetPhoto = nu.Photo,
                             IsConfirm = s.IsConfirm,
                             Type = s.Type,
                             Message = s.Message,
                             UserID = s.UserID
                         };
            return result.OrderByDescending(s => s.SentDate).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<Domain.DTO.SalutationDTO> FindConfirmedSalutationByAccount(string accountName, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name");
            }
            var result = from u in this.ActiveContext.User
                         join s in this.ActiveContext.Salutation
                         on u.UserID equals s.GreetID
                         join nu in this.ActiveContext.User
                         on s.UserID equals nu.UserID
                         where u.AccountName == accountName &&
                         s.IsConfirm == true
                         select new SalutationDTO
                         {
                             SentDate = s.SentDate,
                             SID = s.SID,
                             GreetAccount = nu.AccountName,
                             GreetID = s.GreetID,
                             GreetName = nu.Name,
                             GreetPhoto = nu.Photo,
                             IsConfirm = s.IsConfirm,
                             Type = s.Type,
                             Message = s.Message,
                             UserID = nu.UserID
                         };
            return result.OrderByDescending(s => s.SentDate).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<Domain.DTO.SalutationDTO> FindMyGreet(string accountName, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name");
            }
            var result = from u in this.ActiveContext.User
                         join s in this.ActiveContext.Salutation
                         on u.UserID equals s.GreetID
                         join nu in this.ActiveContext.User
                         on s.UserID equals nu.UserID
                         where u.AccountName == accountName
                         && s.IsConfirm == false
                         select new SalutationDTO
                         {
                             SentDate = s.SentDate,
                             SID = s.SID,
                             GreetAccount = nu.AccountName,
                             GreetID = s.GreetID,
                             GreetName = nu.Name,
                             GreetPhoto = nu.Photo,
                             IsConfirm = s.IsConfirm,
                             Type = s.Type,
                             Message = s.Message,
                             UserID = nu.UserID
                         };
            return result.OrderByDescending(s => s.SentDate).ToPagedList(pageIndex, pageSize);
        }

        public new System.Collections.IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }


        public bool IsGreeted(string account,int greetID)
        {
            if (string.IsNullOrEmpty(account))
            {
                throw new ArgumentNullException("Account");
            }
            var result = from u in this.ActiveContext.User
                         join s in this.ActiveContext.Salutation
                         on u.UserID equals s.UserID
                         where u.AccountName == account
                         && s.GreetID == greetID
                         && s.IsConfirm == true
                         && s.Type == 2
                         select u;
            return result.Count() == 0 ? false : true;

        }
    }
}
